/**
 *@Description 订单详情页
 *@Author zhangnf
 *@Date 2015-09-25
 */
var orderDetailCtrl  = new XFApp.client();
XFApp.client.prototype.orderDetail = {
	successCode : "10000",
	page : null,
	pageGoods : null,
	pageCus : null,
	goodsSearchView : null,
	cusSearchView : null,
	urlItems : {
		saveOrderURL : basePath+"/userCenter/order/saveOrder",
		getOrderByIdURL : basePath+"/userCenter/order/getOrderById",
		getBySkuURL : basePath+"/userCenter/goods/getBySku"
	},
	initData : function(orderId){
		_this = this;
		$.ajax( {  
		    url:_this.urlItems.getOrderByIdURL,
		    data:{id : orderId},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(data.code !=_this.successCode
		    			|| null == data.result){
	        		return;
	        	}
		    	_this.renderDataToView(data.result);
		     },  
		     error : function() {  
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	renderDataToView : function(order){
		$("#orderEditRow").show();
		$("#orderNo").val(order.orderNo);
		
		$("#titleId").val(_this.titleDisplay());
    	$("#saleOrderNo").val(order.saleOrderNo);
    	$("#trackingNumber").val(order.trackingNumber);
    	$("#goodsValue").val(order.goodsValue);
    	$("#consignee").val(order.consignee);
    	$("#consigneeTel").val(order.consigneeTel);
    	$("#consigneePost").val(order.consigneePost);
    	$("#consigneeAdd").val(order.consigneeAdd);
    	$("#consigneeCity").val(order.consigneeCity);
    	$("#orderStatus").val(order.orderStatus);
    	$("#orderStatusStr").text(dksCtrl.dockerstatus.get(order.orderStatus));
    	    	
    	if(1==order.payType){
    		$("#payType0").removeAttr("checked");
    		$("#payType1").prop("checked","true");
    	}
    	
    	$("#currencyHidden").val(order.currency);
    	$("#nationHidden").val(order.consigneeNation);
    	
    	//币制下拉框
    	dictCtrl.dictionary.load("currency","currencyHidden");
    	//国家下拉框
    	dictCtrl.dictionary.load("nation","nationHidden");
    	
    	//商品详情
    	var details = order.orderDetailLst;
    	for(i=0 ;i<details.length; i++){
    		var _html = '';
    		_html += ''
    			+' <tr id="goodsRow'+i+'"> '
    			+'     <td align="center"><input id="orderDetailLst'+i+'_sku" name="orderDetailLst['+i+'].sku" value="'+details[i].sku+'" onchange="orderDetailCtrl.orderDetail.getGoodsInfo(this.value,'+i+')" type="text" class="z-input w_60"/></td> '
    			+'     <td align="center"> '
    			+'     	<input id="orderDetailLst'+i+'_itemName" name="orderDetailLst['+i+'].itemName" value="'+this.getStr(details[i].itemName)+'" type="text" class="z-input w_60"/> '
    			+'     	<input id="orderDetailLst'+i+'_itemno" name="orderDetailLst['+i+'].itemno" value="'+details[i].itemno+'" type="hidden"/> '
    			+'     </td> '
    			+'     <td align="center"><input id="orderDetailLst'+i+'_price" name="orderDetailLst['+i+'].price" value="'+details[i].price+'" type="text" class="z-input w_60"/></td> '
    			+'     <td align="center"><span class="z-input-set"> '
    			+'     	<input id="orderDetailLst'+i+'_qty" name="orderDetailLst['+i+'].qty" value="'+details[i].qty+'" class="z-input" type="text"> '
    			+'     	<div class="input-group"> '
    			+'     	    <input id="orderDetailLst'+i+'_unit" type="text" class="z-input" autocomplete="off" maxlength="20"/> '
    			+'     	    <input id="orderDetailLst'+i+'_unitHidden" name="orderDetailLst['+i+'].unit" value="'+details[i].unit+'" type="text" style="display: none;"> '
    			+'     	    <div class="input-group-btn"> '
    			+'     	        <button type="button" type="button" class="input-group-link" data-toggle="dropdown> '
    			+'     	            <span class="caret"></span> '
    			+'     	        </button> '
    			+'     	        <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul> '
    			+'     	    </div> '
    			+'     	</div> '
    			+'	   </td>'
    			+'     <td align="center"><input id="orderDetailLst'+i+'_brand" name="orderDetailLst['+i+'].brand" value="'+this.getStr(details[i].brand)+'" type="text" class="z-input w_60"/></td> '
    			+'     <td align="center"><input id="orderDetailLst'+i+'_gModel" name="orderDetailLst['+i+'].gModel" value="'+this.getStr(details[i].gModel)+'" type="text" class="z-input w_60"/></td> '
    			+'     <td align="center"><input id="orderDetailLst'+i+'_netWeight" name="orderDetailLst['+i+'].netWeight" value="'+details[i].netWeight+'" type="text" class="z-input w_60"/></td> '
    			+'     <td align="center"><input id="orderDetailLst'+i+'_grossWeight" name="orderDetailLst['+i+'].grossWeight" value="'+details[i].grossWeight+'" type="text" class="z-input w_60"/></td> '
    			+'     <td align="center"> '
    			+'     	<a class="zi-icon zi-del" href="javascript:;" onclick="orderDetailCtrl.orderDetail.deleteRowView('+i+')">删除</a> '
    			+'     	<input id="orderDetailLst'+i+'_isRemoved" name="orderDetailLst['+i+'].isRemoved" value="0" value="'+details[i].isRemoved+'" type="hidden"/> '
    			+'     	<input id="orderDetailLst'+i+'_id" name="orderDetailLst['+i+'].id" value="'+details[i].id+'" type="hidden"/> '
    			+'     </td> '
    			+' </tr> ';
    		$("#orderDetailTable tbody").append(_html);
    		$("#goodsTotalRows").val(i);
    		//单位下拉框
        	dictCtrl.dictionary.load("measureUnit","orderDetailLst"+i+"_unitHidden","orderDetailLst"+i+"_unit");
    	}
	},
	getStr : function(str){
		if(null == str
			|| "null" == str
				|| "NULL" == str){
			return "";
		}else{
			return str
		}
	},
	titleDisplay : function(){
		switch(parseInt($("#orderId").val())){
			case -1: return "创建订单";
			default :return "订单详情";
		}
	},
	formValidate : function(){
		$("#editForm").validate({
	        rules: {
	        	saleOrderNo: "required",
	        	trackingNumber: "required",
	        	goodsValue: {
	        		required: true,
	        		amount:true
	        	},
	        	currencyValidate: "required",
	        	consignee: "required",
	        	consigneeTel: "required",
	        	consigneePost: "required",
	        	nationValidate: "required",
	        	consigneeAdd: "required"
			  },
			 messages: {
				saleOrderNo: "请填写销售订单号",
				trackingNumber: "请填写运单号",
				goodsValue: {
	        		required: "请填写总金额"
	        	},
	        	currencyValidate: "请选择币制",
	        	consignee: "请填写收货人",
	        	consigneeTel: "请填写联系电话",
	        	consigneePost: "请填写邮政编码",
	        	nationValidate: "请选择国家/地区",
	        	consigneeAdd: "请填写收货人地址"
			  }
	    });
		$("#editForm").validate();
	},
	submitForm : function(){
		_this = this;
		$.ajax( {  
		    url:_this.urlItems.saveOrderURL,
		    data:$('#editForm').serialize(),  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	webAlert({
		            title:'提示',
		            content:data.msg,
		            opacity:.5,
		            bg:"gray",
		            width:300,
		            height:10,
		            button:[
		                    {
		                    name:"关闭",
		                    callback:function(){
		                  	  this.close();
		                  	  if(data.isSuccess){
		                  		window.location.href=basePath+"/userCenter/order/index";
		                  	  }
		                    }
		                    }]
		            });
		     },  
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	addGoodsRow : function(){
		var rowIndex =  parseInt($("#goodsTotalRows").val())+1;
		var _html = '';
		_html += ''
			+' <tr id="goodsRow'+rowIndex+'"> '
			+'     <td align="center"><input id="orderDetailLst'+rowIndex+'_sku" name="orderDetailLst['+rowIndex+'].sku" onchange="orderDetailCtrl.orderDetail.getGoodsInfo(this.value,'+rowIndex+')" type="text" class="z-input w_60"/></td> '
			+'     <td align="center"> '
			+'     	<input id="orderDetailLst'+rowIndex+'_itemName" name="orderDetailLst['+rowIndex+'].itemName" type="text" class="z-input w_60"/> '
			+'     	<input id="orderDetailLst'+rowIndex+'_itemno" name="orderDetailLst['+rowIndex+'].itemno" type="hidden"/> '
			+'     </td> '
			+'     <td align="center"><input id="orderDetailLst'+rowIndex+'_price" name="orderDetailLst['+rowIndex+'].price" type="text" class="z-input w_60"/></td> '
			+'     <td align="center"><span class="z-input-set"> '
			+'     	<input id="orderDetailLst'+rowIndex+'_qty" name="orderDetailLst['+rowIndex+'].qty" class="z-input" type="text"> '
			+'     	<div class="input-group"> '
			+'     	    <input id="orderDetailLst'+rowIndex+'_unit" type="text" class="z-input" autocomplete="off" maxlength="20"/> '
			+'     	    <input id="orderDetailLst'+rowIndex+'_unitHidden" name="orderDetailLst['+rowIndex+'].unit" type="text" style="display: none;"> '
			+'     	    <div class="input-group-btn"> '
			+'     	        <button type="button" type="button" class="input-group-link" data-toggle="dropdown> '
			+'     	            <span class="caret"></span> '
			+'     	        </button> '
			+'     	        <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul> '
			+'     	    </div> '
			+'     	</div> '
			+'	   </td>'
			+'     <td align="center"><input id="orderDetailLst'+rowIndex+'_brand" name="orderDetailLst['+rowIndex+'].brand" type="text" class="z-input w_60"/></td> '
			+'     <td align="center"><input id="orderDetailLst'+rowIndex+'_gModel" name="orderDetailLst['+rowIndex+'].gModel" type="text" class="z-input w_60"/></td> '
			+'     <td align="center"><input id="orderDetailLst'+rowIndex+'_netWeight" name="orderDetailLst['+rowIndex+'].netWeight" type="text" class="z-input w_60"/></td> '
			+'     <td align="center"><input id="orderDetailLst'+rowIndex+'_grossWeight" name="orderDetailLst['+rowIndex+'].grossWeight" type="text" class="z-input w_60"/></td> '
			+'     <td align="center"> '
			+'     	<a class="zi-icon zi-del" href="javascript:;" onclick="orderDetailCtrl.orderDetail.deleteRowView('+rowIndex+')">删除</a> '
			+'     	<input id="orderDetailLst'+rowIndex+'_isRemoved" name="orderDetailLst['+rowIndex+'].isRemoved" value="0" type="hidden"/> '
			+'     	<input id="orderDetailLst'+rowIndex+'_id" name="orderDetailLst['+rowIndex+'].id" type="hidden"/> '
			+'     </td> '
			+' </tr> ';
		$("#orderDetailTable tbody").append(_html);
		$("#goodsTotalRows").val(rowIndex);
		//单位下拉框
    	dictCtrl.dictionary.load("measureUnit","orderDetailLst"+rowIndex+"_unitHidden","orderDetailLst"+rowIndex+"_unit");
	},
	getGoodsInfo : function(sku,rowIndex){
		_this = this;
		$.ajax( {  
		    url:_this.urlItems.getBySkuURL,
		    data:{"sku":sku},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(null == data
		    			|| null == data.result){
		    		return;
		    	}
		    	_this.renderDetail(data.result,rowIndex);
		     },  
		     error : function() {
		    	 console.log("提示","操作失败");
		     }  
		});
	},
	renderDetail : function(goods,rowIndex){
		$('#orderDetailLst'+rowIndex+'_itemName').val(goods.itemname);
		$('#orderDetailLst'+rowIndex+'_itemno').val(goods.itemno);
		$('#orderDetailLst'+rowIndex+'_price').val(goods.price);
		$('#orderDetailLst'+rowIndex+'_unitHidden').val(goods.unit);
		$('#orderDetailLst'+rowIndex+'_brand').val(goods.brand);
		$('#orderDetailLst'+rowIndex+'_gModel').val(goods.gmodel);
	},
	deleteRowView : function(rowNum){
		$("#orderDetailLst"+rowNum+"_isRemoved").val("1");
		$("#goodsRow"+rowNum).hide();
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
	},
	event : function(){
		_this = this;
		
		$("#submitBtn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.submitForm();
			}
		});
		$("#addGoodsBtn").on("click",function(){
			_this.addGoodsRow();
		});
		//订单表单验证
		this.formValidate();
	},
};
$(function(){
	//初始化编辑数据
	var orderId = $("#orderId").val();
	if(null != orderId && orderId > 0){
		orderDetailCtrl.orderDetail.initData(orderId);
	}else{
    	//币制下拉框
    	dictCtrl.dictionary.load("currency","currencyHidden");
    	//国家下拉框
    	dictCtrl.dictionary.load("nation","nationHidden");
	}
	
	//初始化事件
	orderDetailCtrl.orderDetail.event();
});